Compiler: Compiler MakeCompiler

Compiler def  MakeProc

Compiler code MakeCode

Compiler : MakeColon

Compiler Python  MakePython

Compiler ;

Python par = []

def pop() :
	global par 
	tos = par[-1] 
	par[-1:] = [] 
	return tos

def push(v) :
	global par
	par += [v]

code .s ( -- )
	global par
	print par

code !s ( -- )
	global par
	par = []



Compiler if 
        newline
        appendcode "if pop\(\):"
        +indent

Compiler then 
        -indent

Compiler else
	-indent
	newline
	appendcode "else:" 
	+indent

Compiler global
        indent
        appendcode "global [GetItem]"
        newline

Objecttype variable   
set variable(instance) {obj = pop()}

set variable() {push( obj ) }
set variable(get) {push( obj ) }
set variable(@) {push( obj ) }
set variable(put) {obj = pop()}
set variable(!) {obj = pop()}
set variable(set) {obj = pop()}
set variable(incr) {obj += 1}
set variable(add) {obj += pop()}
set variable(print) {print obj,}

code dup ( n -- n n )

code swap ( n1 n2 -- n2 n1 )

code over ( n1 n2 -- n1 n2 n1 )

code drop ( n1 n2 -- n1 )

code nip ( n1 n2 -- n2 )

code rot ( n1 n2 n3 -- n2 n3 n1 )

code depth ( -- n )
	n = len(par)

Compiler " 	
	PushText

code . ( text -- )
	print text,

Compiler ."
	PushText
	appendcode "print pop(),"

code cr ( -- )
	print

code .cr ( text -- )
	print text

code space (  -- )
	print '',

code spaces ( n -- )
	print (n-1) * ' ',

Compiler + # ( n1 n2 -- n3 )
	newline 
	appendcode "push(pop()+pop())"

Compiler 1+ # ( n1 -- n2 )
	newline
	appendcode "push(pop()+1)"

Compiler 1- # ( n1 -- n2 )
	newline
	appendcode "push(pop()-1)"

Compiler - # ( n1 n2 -- n3 )
	newline 
	appendcode "push(- pop() + pop())"

Compiler * # ( n1 n2 -- n3 )
	newline 
	appendcode "push(pop()*pop())"

Compiler / # ( n1 n2 -- n3 )
	newline 
	appendcode "tos = pop(); push(pop() / tos)"

Compiler % # ( n1 n2 -- n3 )
	newline 
	appendcode "tos = pop(); push(pop()%tos)"

Compiler int # ( n1 -- n2 )
	newline 
	appendcode "push(int(pop()))"

Compiler min # ( n1 n2 -- n )
	newline 
	appendcode "push(min(pop(),pop()))"

Compiler max # ( n1 n2 -- n )
	newline 
	appendcode "push(max(pop(),pop()))"

Compiler abs # ( n1 n2 -- n )
	newline 
	appendcode "push(abs(pop()))"

code sgn ( n1 -- n2 )
	if n1 > 0:
		n2 = 1
	elif n1 < 0:
		n2 = -1
	else:
		n2 = 0

: test_sub ( a -- b )
	a incr a b !

9 variable test_global

: test_variable ( a | b -- c )
	a b !
	a print	
	a print \ no newline in between, ok.
	cr
	
	a .s test_sub c !

: indent_test ( -- )
	1 dup if drop 2 else drop 44 then

: test_button ( -- )
	1 .s \ do something

: test_dupswaps ( | t -- )
	." --test dupswaps"
	!s
	1 2 3 .s 
	." ! @" cr
	t ! .s t .s
	." drop swap over" cr 
	drop .s
	swap .s
	over .s
	." nip 3 rot depth" cr
	nip .s
	3 rot .s
	depth .s

: test_output ( -- )
	." --Testing output" cr
	1 . 2 . cr
	1 . space 2 . cr
	1 . 2 spaces 2 . cr
	1 . "  " . 2 . cr \ should be 1 spaces?
	" Hi" . " Ho" .
	." Hi" ." Ho"
	" Hi Ho" .cr

: test_arithmetic ( -- )
	." -- test arithmetic" cr
	4 2 + .cr
	5 1+ .cr
	7 1- .cr
	2 3 * .cr
	12 2 / .cr
	20 7 % .cr
	6.6 int .cr
	6 7 min .cr
	6 5 max .cr
	-6 abs .cr
	-6 sgn .cr 6 sgn .cr 0 sgn .cr

." test global: " test_global . cr
." test variable and sub, should give same stack, top incremented" cr
1 test_variable .s
test_dupswaps
test_output
test_arithmetic

: broken ( -- )
	1 drop \ compiler needs some content

